layui.config({
    base: 'dist/sliderVerify/'
}).use(['layer', 'sliderVerify'], function () {
    let layer = layui.layer
        , sliderVerify = layui.sliderVerify;

    /*登录滑块验证*/
    let slider = sliderVerify.render({
        elem: '#slider',
        isAutoVerify: false,//关闭自动验证
        text: '拖动滑块进行验证'
    })


    /*登录功能*/
    $("#loginBtn").click(function () {
        //获取登录名和密码并去除两端的空格
        let userName = $("#userName").val().trim();  //用户名
        let userPass = $("#userPass").val().trim();  //密码
        let role = $("#role").val();  //终端  1是学生   2是老师  3是管理员
        /*密码和用户名做初步的判断*/
        if (userName === null || userName === '' || userName === null || userPass === '') {
            layer.msg('用户名密码不能为空', {offset: '300px', time: 2500, anim: 6});
            return false;
        }
        if (slider.isOk()) {//用于表单验证是否已经滑动成功
            slider.reset();
            login(userName, userPass, role);
        } else {
            layer.msg("请先通过滑块验证");
        }
        return false;
    });

    /*其他登录接口*/
    $("#login-qq").click(function () {
        layer.msg('功能开发中', {offset: '300px', time: 1500, anim: 0});
    });
    $("#login-wechat").click(function () {
        layer.msg('功能开发中', {offset: '300px', time: 1500, anim: 0});
    });
    $("#login-weibo").click(function () {
        layer.msg('功能开发中 ʕ•ﻌ•ʔ', {offset: '300px', time: 1500, anim: 0});
    });
    $("#forgetPss").click(function () {
        layer.msg('功能开发中 ʕ•ﻌ•ʔ', {offset: '300px', time: 1500, anim: 0});
    });
});

//登录功能
function login(name, pass, role) {
    let url, nextPage;  // url 是调用后端的接口的地址   nextPage是登录成功跳转的地址
    //role 终端  1是学生   2是老师  3是管理员
    if (role === 1 || role === '1') {
        url = '/stuLogin';
        nextPage = 'student/index.jsp';
    } else if (role === 2 || role === '2') {
        url = '/teacherLogin';
        nextPage = 'teacher/role.jsp';
    } else if (role === 3 || role === '3') {
        url = '/adminLogin';
        nextPage = 'admin/index.jsp';
    } else {
        layui.layer.alert('出错啦!');
    }
    /*调用后端接口*/
    let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
    $.ajax({
        url: url,
        type: 'post',
        data: {
            userName: name,
            password: pass
        },
        dataType: 'json',
        success: function (data) {  //调用接口成功
            layui.layer.close(loadPage);
            if (data === 1001 || data === '1001') {
                layui.layer.msg('登录成功', {offset: '300px', time: 2500, anim: 0});
                window.location.href = nextPage;
            } else if (data === 1002 || data === '1002') {
                layui.layer.msg('用户名或密码错误', {offset: '300px', time: 2500, anim: 6});
            } else if (data === 1003 || data === '1004') {
                layui.layer.msg('账号不存在', {offset: '300px', time: 2500, anim: 6});
            } else if (data === 1005 || data === '1005') {
                layui.layer.msg('您的账户已登录', {offset: '300px', time: 2500, anim: 6});
            } else {
                layui.layer.msg('登录失败', {offset: '300px', time: 2500, anim: 6});
            }
        },
        error: function () {  //接口错误
            layui.layer.close(loadPage);
            layui.layer.alert('服务器出错啦  (｡•́︿•̀｡)');
        }
    });
}
